package cn.cvs.servlet;

import cn.cvs.pojo.Movie;
import cn.cvs.service.MovieService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/insert")
public class InsertServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        MovieService service = new MovieService();
        Movie movie = new Movie();
        movie.setName(req.getParameter("name"));
        movie.setType(req.getParameter("type"));
        movie.setPrice(Float.parseFloat(req.getParameter("price")));
        movie.setRecommend(Integer.parseInt(req.getParameter("tui")));
        movie.setStartTime(req.getParameter("start"));
        movie.setEndTime(req.getParameter("end"));
        movie.setVotes(Integer.parseInt(req.getParameter("votes")));
        int count = service.intmovie(movie);
        PrintWriter out = resp.getWriter();
        if (count>0){
            out.print("<script>\n" +
                    "    alert(\"添加成功\")\n" +
                    "    location.href=\"list\"\n" +
                    "</script>");
        }else{
            out.print("<script>\n" +
                    "    alert(\"添加失败\")\n" +
                    "    history.back();\n" +
                    "</script>");
        }
    }
}
